package model

import (
	"context"
	"service/ctrl"
	"service/model/mod"

	gpb "gitee.com/cloudapex/protos/hr_cache/pb"

	"google.golang.org/grpc"
	"google.golang.org/grpc/status"
)

var PostsCache postsCache

func init() {
	ctrl.GCon.RegistClienter(mod.GCONN_HR_CACHE, func(c *grpc.ClientConn) { PostsCache.cli = gpb.NewPostsCacheClient(c) })
}

type postsCache struct {
	cli gpb.PostsCacheClient
}

//  postsCache.GetPostList 获取内存的岗位列表
func (this *postsCache) GetPostList(ctx context.Context, in *gpb.PostBaseCacheReq, postId int64) (bool, error) {
	var fage bool
	rsp, err := this.cli.GetPostList(ctx, in)
	if s := status.Convert(err); s.Err() != nil {
		return false, s.Err()
	}
	if len(rsp.PostMap) > 0 {
		for _, v := range rsp.PostMap {
			if v.Id == postId {
				fage = true
			}
		}
	}
	return fage, err
}

// postsCache.AddPost 添加岗位
func (this *postsCache) AddPost(ctx context.Context, in *gpb.PostBaseCacheReq) (*gpb.PostBaseCacheRes, error) {
	return this.cli.AddPost(ctx, in)
}

// postsCache.EditPost 编辑岗位
func (this *postsCache) EditPost(ctx context.Context, in *gpb.PostBaseCacheReq) (*gpb.PostBaseCacheRes, error) {
	return this.cli.EditPost(ctx, in)
}
